<template>
  <div class="tableList">
    <div class="tableW">
      <div class="tableH">
        <tableTrVue :head-data="headData">
          <template slot-scope="{ item }">
            <slot :name="item.hSlot" :item="item">{{ item.label }}</slot>
          </template>
        </tableTrVue>
      </div>
      <div class="tableB">
        <div class="tableTrW" v-for="(e, i) in tableData" :key="i">
          <tableTrVue
            :headData="headData"
            @click.native="nodeClick(e, i)"
            :active="index === i"
          >
            <template slot-scope="{ item }">
              <slot :name="item.slot" :item="item">
                <div class="txt">
                  {{ e[item.prop] }}
                </div>
              </slot>
            </template>
          </tableTrVue>
          <div class="tableTrBtm" v-if="isChild && index === i">
            <slot :item="{ item: e, index: i }"></slot>
          </div>
        </div>
      </div>
    </div>
    <elPaginationVue class="elPaginationVue" @currentChange="currentChange"/>
  </div>
</template>

<script>
import tableTrVue from './tableTr.vue';
import elPaginationVue from '../elPaginationVue/index.vue';

export default {
  name: 'tableList',
  components: { tableTrVue, elPaginationVue },
  props: {
    headData: {
      type: Array,
      default: () => [],
    },
    tableData: {
      type: Array,
      default: () => [],
    },
    pIndex: {
      type: Number,
      default: -1,
    },
    isChild: {
      type: Boolean,
      default: false,
    },
  },
  watch: {
    pIndex(val) {
      this.index = val;
    },
  },
  data() {
    return {
      index: -1,
    };
  },
  methods: {
    nodeClick(e, i) {
      this.index = i;
      this.$emit('nodeClick', { item: e, index: i });
    },
    currentChange(pageNum) {
      this.$emit('currentChange', pageNum);
    },
  },
};
</script>

<style scoped lang="scss">
.tableList {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  .tableW {
    flex: 1;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    .tableH {
      width: 100%;
      background-color: #eee;
    }
    .tableB {
      flex: 1;
      overflow-y: auto;
    }
  }
}
</style>
